import Vue from 'vue'
import App from './App'
import req from 'graceUI/jsTools/request.js'
import ourLoading from '@/components/our-loading/our-loading.vue'
Vue.component('ourLoading', ourLoading)
Vue.prototype.loadings = false;
/**
 * 循环取出数组的键和值
 * callback(值,键)
 */
Vue.prototype.each = (data,callback)=>{
	if(Array.isArray(data)){
		for(var a=0;a<data.length;a++){
			if(callback){
				callback(data[a],a);
			}
		}
	}else{
		for(r in data){
			if(callback){
				callback(data[r],r);
			}
		}
	}
}
//处理不同的数组数据
Vue.prototype.Processing = (data,callback)=>{
	//索引数组
	if(Array.isArray(data)){
		var newdata = new Array();
		for(var a=0;a<data.length;a++){
			if(callback){
				var fanhui = callback(data[a],a);
				newdata.push(fanhui)
			}
		}
		return newdata;
	}else{
		//对象数组
		var newdata = new Array();
		for(r in data){
			if(callback){
				var fanhui = callback(data[r],r);
				newdata[r] = fanhui;
			}
		}
		return newdata;
	}
}
/**
 * 我的请求封装
 */
Vue.prototype.myPost = (url,data,sucesss,fail,fanhui)=>{
	if(fanhui===false){
		fanhui = false;
	}else{
		fanhui = true;
	}
	// console.log(getApp().globalData.url)
	//获取全部的url地址
	url = getApp().globalData.url+url
	//添加app的id参数
	data.merch_id = getApp().globalData.merch_id;
	var user = uni.getStorageSync('storage_key')
	if(user.token){
		data.token = user.token
	}
	
	uni.request({
		url:url,
		method:"POST",
		data:data,
		dataType:"JSON",
		success:function(res){
			if(sucesss){
				sucesss(res.data);
			}
			if(fanhui){
				if(res.data.code!=200){
					if(res.data.msg.length<=6){
						uni.showToast({
							title:res.data.msg,
							icon:'none'
						})
					}else{
						uni.showToast({
							title:res.data.msg,
							icon:'none',
							duration:3000
						})
					}
				}
			}
			
		},
		fail:function(res){
			if(fail){
				fail(res.data)
			}
			if(fanhui){
				uni.showToast({
					title:'系统错误，请联系客服',
					icon:'none',
					duration:2000
				})
			}
		}
	})
}
//上传图片
Vue.prototype.UpFile = (url,callback,data,num,name)=>{
	url = getApp().globalData.url+url
	if(!num){
		num = 1
	}
	if(!name){
		name = 'file'
	}
	if(!data){
		data = {}
	}
	uni.chooseImage({
	    count:num, //默认9
	    sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
	    sourceType: ['album','camera'], //从相册选择
	    success: function (res) {
	        console.log(JSON.stringify(res.tempFilePaths));
			const tempFilePaths = res.tempFilePaths;
			console.log(tempFilePaths)
			for(var v in tempFilePaths){
				console.log(tempFilePaths[v]);
				uni.uploadFile({
					url:url,
					filePath:tempFilePaths[v],
					name:name,
					formData:data,
					success:function(res){
						var data = res.data;
						data = eval('('+data+')');
						console.log(data)
						if(callback){
							callback(data);
						}
					},
					fail:function(){
						uni.showToast({
							title:'上传失败',
							icon:'none'
						})
					}
				})
			}
	    }
	});
}
//判断当前app是否包含某模块
Vue.prototype.getModels = (str)=>{
	if(!str){
		return false;
	}
	var models = uni.getStorageSync('models');
	if(models.indexOf(str)==-1){
		return false;
	}else{
		return true;
	}
}

module.exports = {
	
}
// Vue.prototype.lochost="https://ntgk.xliank.com/"
// Vue.prototype.lochost="https://m.xliank.com/api";
Vue.prototype.sys=uni.getSystemInfoSync();
App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()

